package dataconnect;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Set;

import util.DateUtil;

import analyse.StockAnalyse;
import data.ChineseToPy;
import data.Constant;
import data.HistoryData;
import data.LoadHistoryData;
import data.StockDataFetch;
import data.StockURLDataBuffer;
import data.TimeStock;

public class InsertStockDeal {
	public static void insertStockData(String fileName) throws SQLException{
		
		long t0 = System.currentTimeMillis();
		LoadHistoryData lhd = new LoadHistoryData();
		HistoryData hd = lhd.loadData();
		long t01 = System.currentTimeMillis();
		System.out.println("读取文件："+(t01-t0));
		Set<String> keySet = StockURLDataBuffer.getData().keySet();
		Connection con = ConnectionFactory.getConnection();
		con.setAutoCommit(false);
		Statement stmt = con.createStatement();
		
		String date = fileName.substring(0, 10);
		stmt.execute("delete from stockdaydeal where dealDay = '"+date+"'");
		
		PreparedStatement ps = con.prepareStatement("insert stockdaydeal(id,number,predayprice,price,beginPrice,maxPrice,minPrice,curBuyPrice,amount,qty,dealDay,ratepercent,amountBat) values(?,?,?,?,?,?,?,?,?,?,?,?,?)");
		
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		int i = 0;
		long t02 = System.currentTimeMillis();
		for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext();) {
			String code = iterator.next();
			TimeStock ts = StockDataFetch.getTimeStock(code);
			if(ts==null||fileName.indexOf(ts.getDate().toString())<0){
				i++;
				continue;
			}
			ts.setLastStock(hd.get(code));
			ps.setString(1, code+sdf.format(ts.getDate()));
			ps.setString(2, code);
			ps.setBigDecimal(3, ts.getPreDayPrice());
			ps.setBigDecimal(4, ts.getCurPrice());
			ps.setBigDecimal(5, ts.getBeginPrice());
			ps.setBigDecimal(6, ts.getMaxPrice());
			ps.setBigDecimal(7, ts.getMinPrice());
			ps.setBigDecimal(8, ts.getCurBuyPrice());
			ps.setBigDecimal(9, ts.getAmount());
			ps.setInt(10, ts.getQty());
			ps.setDate(11, ts.getDate());
			ps.setBigDecimal(12, ts.getCurPrice().subtract(ts.getPreDayPrice()).multiply(Constant.ONEHUNDRED).divide(ts.getPreDayPrice(), 2,BigDecimal.ROUND_HALF_UP));
			if (ts.getLastStock() == null
					|| ts.getLastStock().getAmount()
							.compareTo(Constant.ZERO) == 0) {
				ps.setBigDecimal(13, Constant.ZERO);
			}else{
				BigDecimal AmountBat = ts.getAmount().divide(ts.getLastStock().getAmount(), 2,BigDecimal.ROUND_HALF_UP);
				ps.setBigDecimal(13, AmountBat);
			}
			ps.addBatch();
		}
		long t1 = System.currentTimeMillis();
		ps.executeBatch();
		
		con.commit();
		ps.clearBatch();
		ps.close();
		long t2 = System.currentTimeMillis();
		System.out.println("时间："+(t2-t1)+"ms");
		con.setAutoCommit(true);
		
//		new CorrectAmountBat(new Date(DateUtil.parseDate(date).getTime()));
//		long t3 = System.currentTimeMillis();
//		System.out.println("correct bat:"+(t3-t2));
//		
		new StockAnalyse(DateUtil.getThisDate());
		long t4 = System.currentTimeMillis();
		System.out.println("StockAnalyse:"+(t4-t2));
		
		ResultSet rs = stmt.executeQuery("select number from stockdaydeal where dealday = '"+date+"' and number not in (select number from stockinfo)");
		ChineseToPy ctp = new ChineseToPy();
		PreparedStatement ps2 = con.prepareStatement("INSERT INTO stockinfo VALUES (?,?,?,?,?,?,?);");
		while(rs.next()){
			TimeStock ts = StockDataFetch.getTimeStock(rs.getString(1));
			ps2.setString(1,ts.getCode());
			ps2.setString(2, ts.getStockName());
			ps2.setString(3, ctp.getStringFirstPY(ts.getStockName()));
			ps2.setString(4, " ");
			ps2.setString(5, " ");
			ps2.setString(6, " ");
			ps2.setString(7, " ");
			ps2.addBatch();
			System.out.println("new "+ts.getCode()+ts.getStockName());
		}
		ps2.executeBatch();
		ps2.close();
		stmt.close();
	}
}
